﻿// MyNotes - Free Sticky Notes
// Copyright (C) 2011 Alexandre Vicenzi
// 
// This program is free software; you can redistribute it and/or modify it under 
// the terms of the GNU General Public License as published by the Free Software 
// Foundation; either version 3 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with 
// this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace MyNotes
{
    static class Notes
    {
        private static List<Annotation> annotationList = new List<Annotation>();
        private static List<Annotation> garbageList = new List<Annotation>();
        private static string binaryFile = "MyNotes.Annotations.mnb";
        private static string backupFile = "MyNotes.Annotations.Backup.mnb";
        private static string garbageFile = "MyNotes.Annotations.Garbage.mnb";

        public static List<Annotation> AnnotationList
        {
            get { return annotationList; }
        }

        public static List<Annotation> GarbageAnnotationList
        {
            get { return garbageList; }
        }

        public static void AddAnnotation(Annotation note)
        {
            CheckNull(note);
            AnnotationList.Add(note);
            SaveAnnotations();
        }

        public static void RemoveAnnotation(Annotation note)
        {
            CheckNull(note);
            // Create backup.
            CreateBackup();
            // Remove from list.
            AnnotationList.Remove(note);
            // Exclusion date.
            note.ExclusionTime = DateTime.Now;
            // Add to garbage.
            GarbageAnnotationList.Add(note);
            note.DisposeAnnotation();
            // Save.
            SaveAnnotations();
            SaveGarbage();
        }

        private static void CheckNull(Annotation note)
        {
            if (note == null)
                throw new ArgumentNullException("Invalid annotation.");
        }

        public static void LoadAnnotations()
        {
            Deserialize(AnnotationList, binaryFile);
        }

        public static void LoadAnnotationGarbage()
        {
            Deserialize(GarbageAnnotationList, garbageFile);
        }

        public static void SaveAnnotations()
        {
            Serialize(AnnotationList, binaryFile);
        }

        public static void SaveGarbage()
        {
            Serialize(GarbageAnnotationList, garbageFile);
        }

        private static void CreateBackup()
        {
            // Create a backup file before editing.
            File.Copy(binaryFile, backupFile, true);
        }

        private static void Serialize(List<Annotation> list, string file)
        {
            FileStream fs = new FileStream(file, FileMode.Create);
            foreach (Annotation note in list)
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, note);
            }
            fs.Close();
        }

        private static void Deserialize(List<Annotation> list, string file)
        {
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                FileStream fs = new FileStream(file, FileMode.Open);
                while (fs.Position < fs.Length)
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    Annotation note = (Annotation)bf.Deserialize(fs);
                    note.CreateForm();
                    list.Add(note);
                }
                fs.Close();
            }
        }
    }
}
